package com.leetcode.August;

import java.util.Stack;

/**
 * @Description: 394. 字符串解码
 * @Author: Mr.Fxy
 * @CreateTime: 2024-09-13 21:49
 */
public class LC394字符串解码 {
    public String decodeString(String s) {
        Stack<Integer> countStack = new Stack<>();
        Stack<StringBuilder> stringStack = new Stack<>();
        StringBuilder current = new StringBuilder();
        int k = 0;//存储当前读取到的数字
        for (char c : s.toCharArray()) {
            if(Character.isDigit(c)){
                k = k * 10 + c - '0';
            }else if(c=='['){
                countStack.push(k);
                stringStack.push(current);
                current = new StringBuilder();
                k = 0;
            }else if(c==']'){
                StringBuilder temp = current;
                current= stringStack.pop();//拼接所有的字符串
                for (int i = countStack.pop(); i > 0; i--) {
                    current.append(temp);
                }
            }else{
                current.append(c);
            }
        }
        return current.toString();
    }
}
